﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace HexDemo
{
    public class OrthographicCamera
    {
        public Vector2 offset;
		public float zoom = 1.0f;
		public Vector2 viewportDimensions;

        public OrthographicCamera(Vector2 viewportDimensions)
        {
            this.viewportDimensions = viewportDimensions;
        }

        public Matrix Projection
        {
            get
            {
                return Matrix.CreateOrthographic(viewportDimensions.X / zoom, viewportDimensions.Y / zoom, -32, 32);
            }
        }

        public Matrix View
        {
            get
            {
                return Matrix.CreateTranslation(offset.X, offset.Y, 0.0f);
            }
        }

        public Matrix World
        {
            get
            {
                return Matrix.Identity;
            }
        }

        /// <summary>
        /// Transforms a position from screen space to world space.
        /// </summary>
        /// <param name="vec"></param>
        /// <returns></returns>
        public Vector2 UnProject(Vector2 vec)
        {
            vec -= viewportDimensions / 2;
            vec.Y *= -1;
            vec /= zoom;
            vec -= offset;
            return vec;
        }

		public float UnProjectLength(float L)
		{
			return L / zoom;
		}

        /// <summary>
        /// Transforms a position from world space to screen space
        /// </summary>
        /// <param name="vec"></param>
        /// <returns></returns>
        public Vector2 Project(Vector2 vec)
        {
            vec += offset;
            vec *= zoom;
            vec.Y *= -1;
            vec += viewportDimensions / 2;
            return vec;
        }

    }

}
